<?php

	if(current_user_can('manage_options')){
	
	/*-----------------------------------------------------------------------------------*/
	/* Theme Portfolio Structure */
	/*-----------------------------------------------------------------------------------*/
	
	add_action('init', 'portfolio_register');

	function portfolio_register() {

		$labels = array(
			'name' => _x('Projects', 'post type general name'),
			'singular_name' => _x('Portfolio Item', 'post type singular name'),
			'add_new' => _x('Add New', 'portfolio item'),
			'add_new_item' => __('Add New Portfolio Item'),
			'edit_item' => __('Edit Portfolio Item'),
			'new_item' => __('New Portfolio Item'),
			'view_item' => __('View Portfolio Item'),
			'search_items' => __('Search Portfolio'),
			'not_found' =>  __('Nothing found'),
			'not_found_in_trash' => __('Nothing found in Trash'),
			'parent_item_colon' => ''
		);

		$args = array(
			'labels' => $labels,
			'public' => true,
			'publicly_queryable' => true,
			'show_ui' => true,
			'query_var' => true,
			'menu_icon' => get_stylesheet_directory_uri() . '/img/projectsicon.png',
			'rewrite' => true,
			'capability_type' => 'post',
			'hierarchical' => false,
			'menu_position' => null,
			'supports' => array('title','editor','thumbnail')
		  ); 

		register_post_type( 'portfolio' , $args );
	}
	
	//register_taxonomy("presenters", array("portfolio"), array("hierarchical" => true, "label" => "Skills", "singular_label" => "Skill", "rewrite" => true));
	
	add_action( 'init', 'portfolio_init' );
	function portfolio_init()
	{
	    register_taxonomy( 'portfolio', 'portfolio',
	        array(  'hierarchical' => true,
	                'label' => __('Portfolio'),
	                'query_var' => false
	        )
	    );
	}

	add_action( 'portfolio_edit_form_fields', 'edit_portfolio_category', 10, 2);
	// A callback function to add a custom field to our "portfolio" taxonomy
	function edit_portfolio_category($tag) {
	   // Check for existing taxonomy meta for the term you're editing
	    $t_id = $tag->term_id; // Get the ID of the term you're editing
	    $term_meta = get_option( "portfolio_transition_effect_$t_id" ); // Do the check
	?>
	<tr class="form-field">
	        <th scope="row" valign="top"><label for="portfolio_transition_effect">Transition Effect:</label></th>
	        <td>
	            <select name="term_meta[portfolio_transition_effect]" id="term_meta[portfolio_transition_effect]">
	                <option value="fade" <?php 
	                    if ($term_meta['portfolio_transition_effect'] == "fade") echo ' selected="selected";'; ?>>Fade</option>
	                <option value="showhide" <?php 
	                    if ($term_meta['portfolio_transition_effect'] == "showhide") echo ' selected="selected";'; ?>>Resize</option>
	                <option value="disperse" <?php 
	                    if ($term_meta['portfolio_transition_effect'] == "disperse") echo ' selected="selected";'; ?>>Disperse</option>
	                <option value="rows" <?php 
	                    if ($term_meta['portfolio_transition_effect'] == "rows") echo ' selected="selected";'; ?>>Rows Move</option>
	                <option value="seqfade" <?php 
	                    if ($term_meta['portfolio_transition_effect'] == "seqfade") echo ' selected="selected";'; ?>>Sequential Fade</option>
	            </select>
	            <p class="description">Choose the transition effect for the portfolio items.</p>
	        </td>
	    </tr>
	 
	<?php
	}
		
	add_action( 'edited_portfolio', 'save_portfolio_category', 10, 2);
	function save_portfolio_category($term_id, $tt_id)
	{	            
	    if ( isset( $_POST['term_meta'] ) ) {
        $t_id = $term_id;
        $term_meta = get_option( "portfolio_transition_effect_$t_id" );
        $cat_keys = array_keys( $_POST['term_meta'] );
            foreach ( $cat_keys as $key ){
            if ( isset( $_POST['term_meta'][$key] ) ){
                $term_meta[$key] = $_POST['term_meta'][$key];
            }
        }
        //save the option array
        update_option( "portfolio_transition_effect_$t_id", $term_meta );
    }
	}
	
	add_action('admin_menu', 'addMetaBoxP');
	add_action('save_post', 'saveMetaDataP', 10, 2);
	add_action('admin_head', 'embedUploaderCode');

	function addMetaBoxP(){
		global $metaBoxP, $metaBoxP2;
	  add_meta_box($metaBoxP['id'], $metaBoxP['title'], 'createMetaBoxP', 
	    $metaBoxP['page'], $metaBoxP['context'], $metaBoxP['priority']);
	  add_meta_box($metaBoxP2['id'], $metaBoxP2['title'], 'createMetaBoxP2', 
	    $metaBoxP2['page'], $metaBoxP2['context'], $metaBoxP2['priority']);
	}
	
	/*-----------------------------------------------------------------------------------*/
	/* Add Metabox to Portfolio Post */
	/*-----------------------------------------------------------------------------------*/
	 
	//Define the metabox attributes.
	$metaBoxP = array(
	  'id'     => 'arc_projectthumb_metabox',
	  'title'    => 'Project Thumbnail Image',
	  'page'     => 'portfolio',
	  'context'  => 'normal',
	  'priority'   => 'low',
	  'fields' => array(
	    array(
	      'name'   => 'Custom Image',
	      'desc'   => '<em>Recommended photo size: 175 x 167 px</em>',
	      'id'  => 'arc_projectthumb_image',  //value is stored with this as key.
	      'class' => 'image_upload_field',
	      'type'   => 'media'
	    )
	  )
	);
	
	//Metabox 2
	//Define the metabox attributes.
	$metaBoxP2 = array(
	  'id'     => 'arc_slideroptions_metabox',
	  'title'    => 'Project Extra Options',
	  'page'     => 'portfolio',
	  'context'  => 'normal',
	  'priority'   => 'low',
	  'fields' => array(
	  	array(
	      'name'   => 'Large Image Project',
	      'desc'   => 'Check to display the project with a full width image.',
	      'id'  => 'arc_project_large_image',  //value is stored with this as key.
	      'class' => '',
	      'type'   => 'check'
	    ),
	    array(
	      'name'   => 'Project Photos URL',
	      'desc'   => 'Project Photos URL <em>(one per line)</em>:<br><em>Recommended photo size (normal image): 423 x 350 px</em><br><em>Recommended photo size (large image): 763 x 294 px</em>',
	      'id'  => 'arc_project_photos_url',  //value is stored with this as key.
	      'class' => '',
	      'type'   => 'textarea'
	    ),
	    array(
	      'name'   => 'Slider Effect',
	      'desc'   => 'Slider Effect',
	      'id'  => 'arc_project_slider_effect',  //value is stored with this as key.
	      'class' => '',
	      'type'   => 'dropdown'
	    ),
	    array(
	      'name'   => 'Slider Speed',
	      'desc'   => 'Slider Speed <em>(miliseconds)</em>',
	      'id'  => 'arc_project_slider_speed',  //value is stored with this as key.
	      'class' => '',
	      'type'   => 'text',
	      'std' => '5000'
	    ),
	    array(
	      'name'   => 'Slider Control Navigation',
	      'desc'   => 'Check it to add a control navigation to the left top of the slide.',
	      'id'  => 'arc_project_slider_controlnav',  //value is stored with this as key.
	      'class' => '',
	      'type'   => 'check'
	    ),
	    array(
	      'name'   => 'Slider Direction Navigation',
	      'desc'   => 'Check it to add direction navigation arrows to the slide (on mouse over).',
	      'id'  => 'arc_project_slider_directionnav',  //value is stored with this as key.
	      'class' => '',
	      'type'   => 'check'
	    )
	  )
	);
	
	/**
	* Create Metabox HTML.
	*/
	function createMetaBoxP($post) {
	  global $metaBoxP, $metaBoxP2;
	  if (function_exists('wp_nonce_field')) {
	    wp_nonce_field('awd_nonce_action','awd_nonce_field');
	  }
	 
	  foreach ($metaBoxP['fields'] as $field) {
	  	echo '<div class="awdMetaBoxP">';
	    //get attachment id if it exists.
	    $meta = get_post_meta($post->ID, $field['id'], true);
	    switch ($field['type']) {
	      case 'media':
	?>
	        <p><?php echo $field['desc']; ?></p>
	        <div class="awdMetaImage">
	<?php 
	        if ($meta) {
	          echo wp_get_attachment_image( $meta, 'thumbnail', true);
	          $attachUrl = wp_get_attachment_url($meta);
	          echo '<p>URL: <a target="_blank" href="'.$attachUrl.'">'.$attachUrl.'</a></p>';
	        }
	?>    
	        </div><!-- end .awdMetaImage -->
	        <p>
	          <input type="hidden" 
	            class="metaValueField" 
	            id="<?php echo $field['id']; ?>" 
	            name="<?php echo $field['id']; ?>"
	            value="<?php echo $meta; ?>" 
	          /> 
	          <input class="image_upload_button"  type="button" value="Choose File" /> 
	          <input class="removeImageBtn" type="button" value="Remove File" />
	        </p>
	 
	<?php
	      break;
	    }
	    echo '</div> <!-- end .awdMetaBox -->';
	  } //end foreach
	}//end function createMetaBox
	
	/**
	* Create Metabox2 HTML.
	*/
	function createMetaBoxP2($post) {
	  global $metaBoxP2;
	  if (function_exists('wp_nonce_field')) {
	    wp_nonce_field('awd_nonce_action','awd_nonce_field');
	  }
	 
	  foreach ($metaBoxP2['fields'] as $field) {
	    echo '<div class="awdMetaBoxP2"><br>';
	    //get attachment id if it exists.
	    $meta = get_post_meta($post->ID, $field['id'], true);
	    switch ($field['type']) {
	      case 'textarea':
	?>
	        <p><?php echo $field['desc']; ?></p>
	        <p>
	          <textarea id="<?php echo $field['id']; ?>" name="<?php echo $field['id']; ?>" style="width: 650px; height: 190px"><?php echo $meta; ?></textarea> 
	        </p><br>
	 
	<?php
	      break;
	      
	      case 'text':
	?>
	        <p><?php echo $field['desc']; ?></p>
	        <p>
	          <input type="text" id="<?php echo $field['id']; ?>" name="<?php echo $field['id']; ?>" value="<?php echo $meta; ?>" /> 
	        </p><br>
	 
	<?php
	      break;
	      
	      case 'dropdown':
	?>
	        <p><?php echo $field['desc']; ?></p>
	        <p>
	          <select id="<?php echo $field['id']; ?>" name="<?php echo $field['id']; ?>">
					  	<option value="sliceDown" <?php if($meta == 'sliceDown') echo 'selected="selected"'; ?>>sliceDown</option>
					    <option value="sliceDownLeft" <?php if($meta == 'sliceDownLeft') echo 'selected="selected"'; ?>>sliceDownLeft</option>
				  		<option value="sliceUp" <?php if($meta == 'sliceUp') echo 'selected="selected"'; ?>>sliceUp</option>
				  		<option value="sliceUpLeft" <?php if($meta == 'sliceUpLeft') echo 'selected="selected"'; ?>>sliceUpLeft</option>
				  		<option value="sliceUpDown" <?php if($meta == 'sliceUpDown') echo 'selected="selected"'; ?>>sliceUpDown</option>
				  		<option value="sliceUpDownLeft" <?php if($meta == 'sliceUpDownLeft') echo 'selected="selected"'; ?>>sliceUpDownLeft</option>
				  		<option value="fold" <?php if($meta == 'fold') echo 'selected="selected"'; ?>>fold</option>
				  		<option value="fade" <?php if($meta == 'fade') echo 'selected="selected"'; ?>>fade</option>
				  		<option value="random" <?php if($meta == 'random') echo 'selected="selected"'; ?>>random</option>
				  		<option value="slideInRight" <?php if($meta == 'slideInRight') echo 'selected="selected"'; ?>>slideInRight</option>
				  		<option value="slideInLeft" <?php if($meta == 'slideInLeft') echo 'selected="selected"'; ?>>slideInLeft</option>
				  		<option value="boxRandom" <?php if($meta == 'boxRandom') echo 'selected="selected"'; ?>>boxRandom</option>
				  		<option value="boxRain" <?php if($meta == 'boxRain') echo 'selected="selected"'; ?>>boxRain</option>
				  		<option value="boxRainReverse" <?php if($meta == 'boxRainReverse') echo 'selected="selected"'; ?>>boxRainReverse</option>
				  		<option value="boxRainGrow" <?php if($meta == 'boxRainGrow') echo 'selected="selected"'; ?>>boxRainGrow</option>
				  		<option value="boxRainGrowReverse" <?php if($meta == 'boxRainGrowReverse') echo 'selected="selected"'; ?>>boxRainGrowReverse</option>		
					  </select> 
	        </p><br>
	 
	<?php
	      break;
	      
	      case 'check':
	?>
	        <p>
	          <input type="checkbox" id="<?php echo $field['id']; ?>" name="<?php echo $field['id']; ?>" <?php if($meta == 'yes') echo 'checked="checked"'; ?> value="yes" /> <?php echo $field['desc']; ?>
	        </p><br>
	 
	<?php
	      break;
	    }
	    echo '</div> <!-- end .awdMetaBox2 -->';

	  } //end foreach
	}//end function createMetaBox2
	 	 
	function saveMetaDataP($post_id, $post) {
	  //make sure we're saving at the right time.
	  //DOING_AJAX is set when saving a quick edit on the page that displays all posts/pages  
	  //Not checking for this will cause our meta data to be overwritten with blank data.
	  if ( empty($_POST)
	    || !wp_verify_nonce($_POST['awd_nonce_field'],'awd_nonce_action')
	    || $post->post_type == 'revision'
	    || defined('DOING_AJAX' )) {
	    return;
	  }
	 
	  global $metaBoxP, $metaBoxP2;
	  global $wpdb;
	 
	  foreach ($metaBoxP['fields'] as $field) {
	    $value = $_POST[$field['id']];
	 
	    if ($field['type'] == 'media' && !is_numeric($value) ) {
	      //Convert URL to Attachment ID.
	      $value = $wpdb->get_var(
	        "SELECT ID FROM $wpdb->posts 
	         WHERE guid = '$value' 
	         AND post_type='attachment' LIMIT 1");
	    }
	    update_post_meta($post_id, $field['id'], $value);
	  }//end foreach
	  
	   //For metabox2
  foreach ($metaBoxP2['fields'] as $field) {
    $value = $_POST[$field['id']];
    update_post_meta($post_id, $field['id'], $value);
  }//end foreach
	}//end function saveMetaDataN
}
?>